﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace LPV_POS.In
{
    public class PrintCOMs
    {
        Print_Type ePrint_Type;
        /// <summary>
        /// In nhiều máy in 1 lúc
        /// BẾP: Tùy vào cấu hình mà HHoa in tai bep mà nó đã dc cấu hình.
        /// </summary>
        /// <param name="_ePrint_Type"></param>
        public PrintCOMs(Print_Type _ePrint_Type)
        {
            ePrint_Type = _ePrint_Type;
        }

        public static SerialPort_LPV[] gsrlCOMBillport;

        #region "   local Property"
        string Print_Align_Center = ((char)(27)).ToString() + ((char)(97)).ToString() + ((char)(1)).ToString();
        string Print_Align_Left = ((char)(27)).ToString() + ((char)(97)).ToString() + '\0';
        //tohoai
        string Print_Font_Double_Height = ((char)(27)).ToString() + ((char)(33)).ToString() + ((char)(16)).ToString();
        string Print_Font_Normal = ((char)(27)).ToString() + ((char)(33)).ToString() + ((char)(2)).ToString();//_40CharPerRow
        string Print_Font_Small = ((char)(27)).ToString() + ((char)(33)).ToString() + ((char)(1)).ToString();//_40CharPerRow

        string Print_BreakPage = ((char)(29)).ToString() + ((char)(86)).ToString() + ((char)(66)).ToString() + ((char)(1)).ToString();

        //  int iSpace1 = 32, iSpace2 = 7, iSpace3 = 17;   // = 56
        // tohoai
        int iSpace1 = 26, iSpace2 = 5, iSpace3 = 11;   // = 42
        #endregion

        #region "   Enum Format"
        #region "   Enum Fonts"
        public enum Fonts
        {
            Font_Small,
            Font_Normal,
            Font_Double_Height
        }
        #endregion

        #region "   Enum aligns"
        public enum aligns
        {
            Align_Center,
            Align_Left
        }
        #endregion

        #region "   Enum Width"
        public enum Fills
        {
            Fills_Full,
            Fills1Row3,
            Fills2Row3
        }
        #endregion

        #region "   Enum Print_Type"
        public enum Print_Type
        {
            Print_BILL,
            Print_KitChen
        }
        #endregion
        #endregion

        #region "   1.SerialPort[] Print_Start"
        /// <summary>
        /// Print_Start()        
        ///  - BILL: hien thi from frmLuaChon_MayIn;
        ///  - KITCHEN: tuy theo MaHH ma HH duoc IN o KITCHEN nao. da duoc CONFIG may in cho DM_HH trong danh muc
        /// </summary>
        /// <returns>Return </returns>
        public SerialPort_LPV[] Print_Start()
        {
            try
            {

                if (ePrint_Type == Print_Type.Print_BILL)
                {
                    Variable oVariable = new Variable();
                    //1. Load thông tin máy in
                    oVariable.Get_DM_MAYIN_HD();

                    //2. Mở cổng máy in
                    oVariable.SerialPort_Open();
                }
                else if (ePrint_Type == Print_Type.Print_KitChen)
                {
                    Variable oVariable = new Variable();
                    //1. Load thông tin máy in
                    oVariable.Get_DM_MAYIN_BEP();//

                    //2. Mở cổng máy in
                    return oVariable.SerialPort_Open_KitChen();
                }
            }
            catch (Exception ex)
            {
                if (new cpublic().Registry_Get("chkCrystalReport_Display") + "" == "True")
                    MessageBox.Show(ex.Message);
                string s = ex.Message;
            }
            return Variable.gsrlCOMBillport;
        }
        #endregion

        #region "   1.SerialPort[] Print_Start"
        public SerialPort_LPV Print_Start_Kitchen(DataRow drBep)
        {
            try
            {
                Variable oVariable = new Variable();
                //2. Mở cổng máy in
                return oVariable.SerialPort_Open_KitChen(drBep);
            }
            catch (Exception ex)
            {
                if (new cpublic().Registry_Get("chkCrystalReport_Display") + "" == "True")
                    MessageBox.Show(ex.Message);
                string s = ex.Message;
            }
            return new SerialPort_LPV();
        }
        #endregion

        #region "   2.Print_Content"
        public void Print_Content(SerialPort_LPV _COMport, aligns _align, Fonts _Font, string _Content)
        {
            try
            {
                this.Write_Align(_COMport, _align);

                this.Write_Font(_COMport, _Font);
                string _Contents = this.str2Column(_Font, _Content, "");
                _COMport.WriteLine(_Contents.Trim());
            }
            catch (Exception ex)
            {
                if (new cpublic().Registry_Get("chkCrystalReport_Display") + "" == "True")
                    MessageBox.Show(ex.Message);
                string s = ex.Message;
            }
        }
        public void Print_Content(SerialPort_LPV _COMport, aligns _align, Fonts _Font, string _Content1, string _Content2, string _Content3, string _Content4)
        {
            try
            {
                this.Write_Align(_COMport, _align);
                this.Write_Font(_COMport, _Font);
                string _Contents = this.str4Column(_Font, _Content1, _Content2, _Content3, _Content4);//789
                _COMport.WriteLine(_Contents.Trim());
            }
            catch (Exception ex)
            {
                if (new cpublic().Registry_Get("chkCrystalReport_Display") + "" == "True")
                    MessageBox.Show(ex.Message);
                string s = ex.Message;

            }
        }
        public void Print_Content(SerialPort_LPV _COMport, aligns _align, Fonts _Font, string _Content1, string _Content2, string _Content3, string _Content4, string space_sign)
        {

            this.Write_Align(_COMport, _align);

            this.Write_Font(_COMport, _Font);
            string _Contents = this.str4Column(_Font, _Content1, _Content2, _Content3, _Content4, space_sign);
            _COMport.WriteLine(_Contents.Trim());
        }
        public void Print_Content5(SerialPort_LPV _COMport, aligns _align, Fonts _Font, string _Content1, string _Content2, string _Content3, string _Content4, string _Content5, string space_sign)
        {

            this.Write_Align(_COMport, _align);

            this.Write_Font(_COMport, _Font);
            string _Contents = this.str5Column(_Font, _Content1, _Content2, _Content3, _Content4, _Content5, space_sign);
            _COMport.WriteLine(_Contents.Trim());
        }
        public void Print_Content(SerialPort_LPV _COMport, aligns _align, Fonts _Font, string _Content1, string _Content2, string _Content3, string _Content4, string _Content5, string _Content6)
        {

            this.Write_Align(_COMport, _align);

            this.Write_Font(_COMport, _Font);
            string _Contents = this.str6Column(_Font, _Content1, _Content2, _Content3, _Content4, _Content5, _Content6);
            _COMport.WriteLine(_Contents.Trim());
        }
        #endregion

        #region "   3.Print_Line"
        public void Print_Line(SerialPort_LPV _COMport, aligns _align, Fonts _Font, char chr, Fills _Fills)
        {
            try
            {
                //chr: "=", "-"
                this.Write_Align(_COMport, _align);
                this.Write_Font(_COMport, _Font);
                int ichar = 42;// 51;// 56;
                if (_Fills == Fills.Fills1Row3)
                    ichar = ichar / 3;
                else if (_Fills == Fills.Fills2Row3)
                    ichar = ichar * 2 / 3;

                _COMport.WriteLine(new String(chr, ichar).Trim());
            }
            catch (Exception ex)
            {
                if (new cpublic().Registry_Get("chkCrystalReport_Display") + "" == "True")
                    MessageBox.Show(ex.Message);
                string s = ex.Message;
            }
        }
        #endregion

        #region "   4.Print_Close"
        /// <summary>
        /// Print_Bottom
        /// Print_BreakPage
        /// </summary>
        /// <param name="_COMport"></param>
        public void Print_Bottom(SerialPort_LPV _COMport)
        {
            try
            {
                _COMport.WriteLine("\n");
                _COMport.WriteLine(Print_BreakPage);
            }
            catch (Exception ex)
            {
                if (new cpublic().Registry_Get("chkCrystalReport_Display") + "" == "True")
                    MessageBox.Show(ex.Message);
                string s = ex.Message;
            }
        }
        #endregion

        #region "   5.Print_Close"
        public void Print_Close()
        {
            try
            {
                if (ePrint_Type == Print_Type.Print_BILL)
                    new Variable().SerialPort_Close();
                else if (ePrint_Type == Print_Type.Print_KitChen)
                    new Variable().SerialPort_Close_KitChen();
            }
            catch (Exception ex)
            {
                if (new cpublic().Registry_Get("chkCrystalReport_Display") + "" == "True")
                    MessageBox.Show(ex.Message);
                string s = ex.Message;
            }
        }
        #endregion

        #region "   strSetting"

        #region "   str6Column"
        string str6Column(Fonts _Font, string a, string b, string c, string d, string e, string f, string space_sign)
        {
            if (space_sign != "")
                return str6Column(_Font, a, b, c, d, e, f).Replace(" ", space_sign);
            else
                return str6Column(_Font, a, b, c, d, e, f);
        }
        string str6Column(Fonts _Font, string a, string b, string c, string d, string e, string f)
        {//iSpace1: 32; iSpace2:7; iSpace3: 17
            cpublic ocpublic = new cpublic();
            a = ocpublic.RemoveSign4VietnameseString(a);
            b = ocpublic.RemoveSign4VietnameseString(b);
            c = ocpublic.RemoveSign4VietnameseString(c);
            d = ocpublic.RemoveSign4VietnameseString(d);
            e = ocpublic.RemoveSign4VietnameseString(e);
            f = ocpublic.RemoveSign4VietnameseString(f);

            string str = "";
            str = a;
            str += new cpublic().Spacestring(6 - a.Length) + "|";
            str += new cpublic().Spacestring(11 - b.Length);
            str += b + "|";
            str += new cpublic().Spacestring(8 - c.Length);
            str += c + "|";
            str += new cpublic().Spacestring(8 - d.Length);
            str += d + "|";
            str += new cpublic().Spacestring(8 - e.Length);
            str += e + "|";
            str += new cpublic().Spacestring(8 - f.Length);
            str += f;

            if (b == "" && c == "" && d == "")
                str = str.Trim();
            return str;
        }
        #endregion


        #region "   str5Column"
        string str5Column(Fonts _Font, string a, string b, string c, string d, string e, string space_sign)
        {
            if (space_sign != "")
                return str5Column(_Font, a, b, c, d, e).Replace(" ", space_sign);
            else
                return str5Column(_Font, a, b, c, d, e);
        }
        string str5Column(Fonts _Font, string a, string b, string c, string d, string e)
        {//iSpace1: 32; iSpace2:7; iSpace3: 17

            cpublic ocpublic = new cpublic();
            a = ocpublic.RemoveSign4VietnameseString(a);
            b = ocpublic.RemoveSign4VietnameseString(b);
            c = ocpublic.RemoveSign4VietnameseString(c);
            d = ocpublic.RemoveSign4VietnameseString(d);
            e = ocpublic.RemoveSign4VietnameseString(e);

            string str = "";
            str = a;
            str += new cpublic().Spacestring(6 - a.Length) + "";
            str += new cpublic().Spacestring(11 - b.Length);
            str += b + "";
            str += new cpublic().Spacestring(8 - c.Length);
            str += c + "";
            str += new cpublic().Spacestring(8 - d.Length);
            str += d + "";
            str += new cpublic().Spacestring(8 - e.Length);
            str += e + "";

            if (b == "" && c == "" && d == "")
                str = str.Trim();
            return str;
        }
        #endregion

        #region "   str4Column"
        string str4Column(Fonts _Font, string a, string b, string c, string d, string space_sign)
        {
            if (space_sign != "")
                return str4Column(_Font, a, b, c, d).Replace(" ", space_sign);
            else
                return str4Column(_Font, a, b, c, d);
        }
        string str4Column(Fonts _Font, string a, string b, string c, string d)
        {//iSpace1: 32; iSpace2:7; iSpace3: 17
            cpublic ocpublic = new cpublic();
            a = ocpublic.RemoveSign4VietnameseString(a);
            b = ocpublic.RemoveSign4VietnameseString(b);
            c = ocpublic.RemoveSign4VietnameseString(c);
            d = ocpublic.RemoveSign4VietnameseString(d);
            if (b == "" && c == "" && d != "")
                return this.str2Column(_Font, a, d);
            // chinh do rong cua cot
            string str = "";
            str = a;
            str += new cpublic().Spacestring(iSpace1 - a.Length - b.Length);
            str += b;
            str += new cpublic().Spacestring(iSpace2 - c.Length);
            str += c;
            str += new cpublic().Spacestring(iSpace3 - d.Length);
            str += d;
            if (b == "" && c == "" && d == "")
                str = str.Trim();
            return str;
        }
        #endregion

        #region "   str2Column"
        string str2Column(Fonts _Font, string a, string b)
        {//i: 56
            int i = 51;
            if (_Font == Fonts.Font_Double_Height)
                i = 42;

            string str = "";
            str = a;
            str += new cpublic().Spacestring(i - a.Length - b.Length);
            str += b;
            return str;
        }
        #endregion

        #region "   Write_Align"
        void Write_Align(SerialPort_LPV _COMport, aligns _align)
        {
            string strAlign = Print_Align_Center;
            if (_align == aligns.Align_Left)
                strAlign = Print_Align_Left;
            _COMport.Write(strAlign);
        }
        #endregion

        #region "   Write_Font"
        void Write_Font(SerialPort_LPV _COMport, Fonts _Font)
        {
            string strFont = Print_Font_Normal;
            if (_Font == Fonts.Font_Double_Height)
                strFont = Print_Font_Double_Height;
            else if (_Font == Fonts.Font_Small)
                strFont = Print_Font_Small;

            _COMport.Write(strFont);
        }
        #endregion
        #endregion


    }
}